package com.tl.sboot.web;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tl.sboot.service.RedisService;
import com.tl.sboot.utils.FileUtil;
import com.tl.sboot.utils.RPOUtil;
import com.tl.sboot.utils.RedisUtil;

@Controller
public class RedisController {
	@Autowired
	RedisService redisService;
	@Autowired
	RedisUtil ru;
	
	/**
	 * 测试redis添加用户
	 * @return
	 */
	@RequestMapping(value="setUserRedis")
	@ResponseBody
	public String setUserRedis(){
		String set = ru.set("cjw", "chenjunwen");
		return set;
	}
	/**
	 * 获取数据
	 * @return
	 */
	@RequestMapping(value="getA_Stock")
	@ResponseBody
	public Set<String> getA_Stock(){
		return ru.zrange("SS.QUANT.DKBF.20161124.SSE.A_STOCK", 0, -1);
	}
	/**
	 * 添加上市公司基本信息表数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addSCPRedis")
	public Map<String, Set<String>> findSTK_COM_PROFILE() {
		return redisService.findSTK_COM_PROFILE();
	}
	/**
	 * 获取这个表所有字段的count
	 * @return
	 */
	@RequestMapping(value="findCountCvs")
	@ResponseBody
	public Map<String, Object> findSTK_CASH_GENC_Count() {
		redisService.findSTK_BALA_GENB_Count();
		redisService.findSTK_INCOME_GENP_Count();
		return redisService.findSTK_CASH_GENC_Count();
	}
	
	/**
	 * 求两个集合的交集
	 * @param REGION
	 * @param GICS_INDU
	 * @return
	 */
	@RequestMapping(value="interSTOCKCODE")
	@ResponseBody
	public Set<String> interSTOCKCODE(String city,String gics_ind){
		return redisService.interSTOCKCODE(city, gics_ind);
	};
	/**
	 * 初始化城市
	 * @return
	 */
	@RequestMapping(value="initCITY")
	@ResponseBody
	public Map<String, String> initCITY(){
		Map<String, String> hgetall = ru.hgetall("CITY");
		return hgetall;
	}
	/**
	 * 初始化行业
	 * @return
	 */
	@RequestMapping(value="initGICS_INDU")
	@ResponseBody
	public List<Map<String, String>> initGICS_INDU(String city){
		return redisService.findGicsByCity(city);
	}
	/**
	 * 初始化表选项
	 * @return
	 */
	@RequestMapping(value="initTables")
	@ResponseBody
	public Map<String, Set<String>> initTables() {
		Set<String> keys = ru.keys("SS.资产负债表.*");
		Set<String> keys2 = ru.keys("SS.现金流量表.*");
		Set<String> keys3 = ru.keys("SS.利润表.*");
		Map<String,Set<String>> map = new HashMap<>();
		map.put("资产负债表", keys);
		map.put("现金流量表", keys2);
		map.put("利润表", keys3);
		return map;
	}
	
	/**
	 * 运算结果
	 * @param param
	 */
	@RequestMapping(value="operation")
	@ResponseBody
	public Set<String> operation(String param){
		Set<String> retainSet = new HashSet<>();//用来求集合交集
		Set<String> retainStrategy = new HashSet<>();
		//处理策略
		int clStart = param.indexOf("CL(");
		int clEnd = param.indexOf(")CL");
		if (clStart!=-1 && clEnd!=-1) {
			String CL = param.substring(clStart+3,clEnd);
			String[] clParam = CL.split(",");
			for (int i=0;i<clParam.length;i++) {
				String key = "SS.QUANT."+clParam[i].trim()+".20161213.SSE.A_STOCK";
				Set<String> strategy  = ru.connectServer().zrangeByScore(key, 0,9000);   
				if (retainStrategy.isEmpty()) {
					retainStrategy = new HashSet<>();
					break;
				}
				if (i==0) {
					retainStrategy.addAll(strategy);
				}else{
					retainStrategy.retainAll(strategy);
				}
			}
			param = param.substring(0,clStart)+param.substring(clEnd+3);
		}
		
		//处理财务
		String interFieldName = "";
		int cwStart = param.indexOf("CW(");
		int cwEnd = param.indexOf(")CW");
		if (cwStart!=-1 && cwEnd!=-1) {
			String CW = param.substring(cwStart+3,cwEnd);
			interFieldName = RPOUtil.operation(CW, ru);//返回一个名称
			param = param.substring(0,cwStart)+param.substring(cwEnd+3);
		}
		String[] params = param.split(",");
		//处理其他的运算
		String min = null;
		boolean cxg = false;
		String max = null;
		String region = null;
		String gics_ind = null;
		for (int i =0;i<params.length;i++) {
			String string = params[i].trim();
			if (string.equals("DESC")||string.equals("ASC")) {
				//sort = string;
			}else if(string.indexOf("min:")!=-1) {
				min = string.substring(4);
			}else if(string.indexOf("max:")!=-1) {
				max = string.substring(4);
			}else if (string.indexOf("region:")!=-1) {
				region = string.substring(7);
			}else if (string.indexOf("gics_ind:")!=-1) {
				gics_ind = string.substring(9);
			}else if (string.equals("CXG")) {//判断是否选择次新股
				cxg = true;
			}
		}
		Set<String> interSet = null;//表代码集合
		Set<String> interSTOCKCODE = null;//行业地区股票代码集合

		if(min!=null && max!=null){
			interSet = ru.zrangebyscore(interFieldName, max, min);
		}else{
			interSet = ru.zrange(interFieldName, 0, -1);
		}
		retainSet.addAll(interSet);
		if (interSet.isEmpty()) {
			retainSet.add("未找到合适的股票代码");
			return retainSet;
		}
		//得到地区行业的股票代码
		if (region!=null && gics_ind!=null) {
			interSTOCKCODE = redisService.interSTOCKCODE(region, gics_ind);
		}else if (region!=null) {
			interSTOCKCODE = ru.smembers("SE.CITY."+region);
		}else if (gics_ind!=null) {
			interSTOCKCODE = ru.smembers("SE.GICS_INDU."+gics_ind);
		}
		
		//与地区行业股票代码取交集
		if (null!=interSTOCKCODE) {
			retainSet.retainAll(interSTOCKCODE);
		}
		//与策略代码取交集
		if(clStart!=-1){
			retainSet.retainAll(retainStrategy);
		}
		//与次新股取交集
		if (cxg) {
			if (!ru.exists("SS.CXG")) {
				redisService.addCXGToRedis();
			}
			Set<String> cxgSet = ru.smembers("SS.CXG");
			retainSet.retainAll(cxgSet);
		}
		/*删除临时存储在redisKey*/
		Set<String> keys = ru.keys("UUID.*");
		String[] keyArr = new String[keys.size()];
		keys.toArray(keyArr);
		ru.del(keyArr);
		if (retainSet.isEmpty()) {
			retainSet.add("未找到合适的股票信息");
		}
		return retainSet;
	}
	

	
	
	@RequestMapping(value="createStrategy")
	public String createStrategy(String text,String fileName){
		//创建本地文件
		FileUtil.createFile(fileName, text,"txt");
		//将文件名称存储到redis中
		ru.sadd("file", fileName);
		return "redirect:blocklyDemo3";
	}
	
	/**
	 * 返回Redis存储的策略名称
	 * @return
	 */
	@RequestMapping(value="findRedisStrategy")
	@ResponseBody
	public Set<String> findRedisStrategy(){
		Set<String> fileSet = ru.smembers("file");
		return fileSet;
	}
	
	
}
